//
//  XLDeviceKit.m
//  XLDeviceModel
//
//  Created by ilongge on 2023/8/17.
//

#import "XLDeviceKit.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>

@implementation XLDeviceKit

+ (NSString *)allDeviceInfo {
    
    NSString *deviceType = [self deviceType];
    NSString *systemVersion = [self getSystemVersion];
    NSString *iPhoneName = [self getiPhoneName];
    NSString *systemName = [self getSystemName];
    NSString *deviceLanguage = [self getDeviceLanguage];
    NSString *batteryState = [self getBatteryState];
    NSString *batteryLevel = [@((int)([self getBatteryLevel] * 100)) stringValue];
    batteryLevel = [NSString stringWithFormat:@"%@%@", batteryLevel, @"%"];
    NSString *UUID = [self getUUID];
    NSString *APPVerion = [self getAPPVerion];
    
    NSString *deviceInfo = [NSString stringWithFormat:@"设备型号:%@,系统版本号:%@,iPhone名称:%@,系统名称:%@,当前语言:%@,电池当前的状态:%@,电池电量:%@,识别码UUID:%@,app版本号:%@;JFVideoDeviceInfo",
                            deviceType,
                            systemVersion,
                            iPhoneName,
                            systemName,
                            deviceLanguage,
                            batteryState,
                            batteryLevel,
                            UUID,
                            APPVerion];
    
    return deviceInfo;
}
+(NSString *)deviceType
{
    return [UIDevice xl_currentDeviceModel];
}
/// 获取iPhone名称
+ (NSString *)getiPhoneName {
    return [UIDevice currentDevice].name;
}

/// 获取app版本号
+ (NSString *)getAPPVerion {
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

/// 获取电池电量
+ (float)getBatteryLevel {
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    return device.batteryLevel;
}

/// 当前系统名称
+ (NSString *)getSystemName {
    return [UIDevice currentDevice].systemName;
}

/// 当前系统版本号
+ (NSString *)getSystemVersion {
    return [UIDevice currentDevice].systemVersion;
}

/// 通用唯一识别码UUID
+ (NSString *)getUUID {
    return [[UIDevice currentDevice] identifierForVendor].UUIDString;
}

/// 获取电池当前的状态，共有4种状态
+ (NSString *)getBatteryState {
    UIDevice *device = [UIDevice currentDevice];
    device.batteryMonitoringEnabled = YES;
    if (device.batteryState == UIDeviceBatteryStateUnknown) {
        return @"UnKnow";
    } else if (device.batteryState == UIDeviceBatteryStateUnplugged){
        return @"Unplugged";
    } else if (device.batteryState == UIDeviceBatteryStateCharging){
        return @"Charging";
    } else if (device.batteryState == UIDeviceBatteryStateFull){
        return @"Full";
    }
    return nil;
}

/// 获取当前语言
+ (NSString *)getDeviceLanguage {
    NSArray *languageArray = [NSLocale preferredLanguages];
    return [languageArray objectAtIndex:0];
}

+ (NSString *)getNetwork {
    NSString *network = @"";
    XLNetworkType status = [self getNetworkType];
    switch (status) {
        case XLNetworkTypeNotReachable: network = @"无网络"; break;
        case XLNetworkTypeReachableViaWWANGPRS: network = @"GPRS"; break;
        case XLNetworkTypeReachableViaWWAN2G: network = @"2G"; break;
        case XLNetworkTypeReachableViaWWANHRD: network = @"HRPD"; break;
        case XLNetworkTypeReachableViaWWAN3G: network = @"3G"; break;
        case XLNetworkTypeReachableViaWWAN4G: network = @"4G"; break;
        case XLNetworkTypeReachableViaWWAN5G: network = @"5G"; break;
        case XLNetworkTypeReachableViaWWAN5GNSA: network = @"5G NSA"; break;
        case XLNetworkTypeReachableViaWWAN: network = @"蜂窝网络"; break;
        case XLNetworkTypeReachableViaWiFi: network = @"WiFi"; break;
        case XLNetworkTypeUnknown: network = @"未知网络"; break;
    }
    return network;
}

+ (XLNetworkType)getNetworkType {
    
    XLNetworkType returnValue = XLNetworkTypeNotReachable;
    
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    NSString *currentStatus = nil;
    if (@available(iOS 12.1, *)){
        currentStatus = info.serviceCurrentRadioAccessTechnology.allValues.lastObject;
    }
    else{
        currentStatus = info.currentRadioAccessTechnology;
    }
    
    if ([currentStatus isEqualToString:CTRadioAccessTechnologyGPRS]) {
        returnValue = XLNetworkTypeReachableViaWWANGPRS;
    }
    else if ([currentStatus isEqualToString:CTRadioAccessTechnologyEdge]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
        returnValue = XLNetworkTypeReachableViaWWAN2G;
    }
    else if ([currentStatus isEqualToString:CTRadioAccessTechnologyWCDMA]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyHSDPA]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyHSUPA]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]
             || [currentStatus isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]){
        returnValue = XLNetworkTypeReachableViaWWAN3G;
    }
    else if ([currentStatus isEqualToString:CTRadioAccessTechnologyeHRPD]){
        returnValue = XLNetworkTypeReachableViaWWANHRD;
    }
    else if ([currentStatus isEqualToString:CTRadioAccessTechnologyLTE]){
        returnValue = XLNetworkTypeReachableViaWWAN4G;
    }
    else if (@available(iOS 14.1, *)) {
        if ([currentStatus isEqualToString:CTRadioAccessTechnologyNRNSA]){
            returnValue = XLNetworkTypeReachableViaWWAN5GNSA;
        }
        else if ([currentStatus isEqualToString:CTRadioAccessTechnologyNR]){
            returnValue = XLNetworkTypeReachableViaWWAN5G;
        }
    }
    return returnValue;
}

@end
